﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Delivery
{
    public partial class DeliveryConfirm : Form
    {
        public DeliveryConfirm()
        {
            InitializeComponent();
        }

        DataTable DtOut;

        //public void BindCMB()
        //{
        //    DtOut = Bll_ProductOut.GetOutStockList();
        //    cmb_BillNo.Text = "";
        //    cmb_BillNo.Items.Clear();
        //    if (DtOut.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in DtOut.Rows)
        //        {
        //            cmb_BillNo.Items.Add(dr["BillNo"].ToString());
        //        }
        //    }
        //}

        private void DeliveryConfirm_Load(object sender, EventArgs e)
        {
            //BindCMB();
        }

        private void cmb_BillNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputBillNo();
        }

        public void InputBillNo()
        {
            DtOut = Bll_ProductOut.GetOutStockListConfirm(cmb_BillNo.Text);
            DataRow[] drOut = DtOut.Select(string.Format(" BillNo = '{0}'", cmb_BillNo.Text));
            if (drOut.Length > 0)
            {
                DataTable info = Bll_ProductOut.GetOutStockInfo(cmb_BillNo.Text);
                if (info.Rows.Count > 0)
                {
                    if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                    {
                        var dt = Bll_ProductOut.GetDeliveryInfo(cmb_BillNo.Text);
                        lab_msg.Text = GetMessage(dt);
                    }
                    else
                    {
                        MessageBox.Show(cmb_BillNo.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");
                        cmb_BillNo.Text = "";
                        cmb_BillNo.Focus();
                    }
                }
            }
            else
            {
                lab_msg.Text = cmb_BillNo.Text + "单号不正确,请输入正确的单据!";
                cmb_BillNo.Text = "";
                cmb_BillNo.Focus();
            }
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            var billNo = cmb_BillNo.Text;

            if (string.IsNullOrEmpty(billNo))
            {
                MessageBox.Show("请先选择单号！");
                cmb_BillNo.Focus();
                return;
            }
            var relatedBill = string.Empty;

            var result = Bll_ProductOut.OutStockDeliveryConfirm(billNo, out relatedBill);
            if (result)
            {
                //if (relatedBill.StartsWith("VMI"))
                //{
                //    Bll_ProductOut.OutStockVMIDelivery(billNo);
                //}

                lab_msg.Text = cmb_BillNo.Text + "发运确认成功!";
                //BindCMB();

            }
            else
            {
                lab_msg.Text = cmb_BillNo.Text + "发运确认失败!";

            }
        }

        public string GetMessage(DataTable dt)
        {
            var result = string.Empty;

            var boxCount = Bll_ProductOut.GetOutStockBoxsCount(cmb_BillNo.Text);

            if (dt.Rows.Count > 0)
            {
                var customerName = dt.Rows[0]["CustomerName"].ToString();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("客户:" + customerName);

                sb.AppendLine("已扫描箱数:" + boxCount);
                foreach (DataRow dr in dt.Rows)
                {
                    sb.AppendLine("物料号:" + dr["ProductNumber"] + ";计划数量:" + dr["PlanQty"] + ";实际数量:" + dr["SelectQty"]);
                }
                result = sb.ToString();

            }
            return result;
        }

        private void cmb_BillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InputBillNo();
            }
        }

    }
}